<template>
  <div class="item">
    <i class="el-icon-user-solid" />
    <div class="title">{{ data.name }}</div>
    <div class="user">{{ data.manager }}</div>
    <el-dropdown @command="commandEvent">
      <span>操作
        <i class="el-icon-arrow-down el-icon--right" />
      </span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="add">添加子部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!data.treeShow"
            command="lock"
          >查看部门</el-dropdown-item>
          <el-dropdown-item
            v-if="!data.treeShow"
            command="del"
          >删除部门</el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script>
import { companyDepartmentDelete } from '@/api/departments.js'
export default {
  // 接收父组件传递过来的参数
  props: {
    data: {
      type: Object,
      default: () => ({}),
      required: true
    }
  },
  methods: {
    async commandEvent(command) {
      switch (command) {
        // 新增
        case 'add':
          this.$bus.$emit('openAdd', true, this.data, 'add') // bus触发打开弹框
          break
        // 编辑
        case 'lock':
          this.$bus.$emit('openAdd', true, this.data, 'lock') // bus触发打开弹框
          break
        // 删除
        case 'del':
          // 提示用户
          await this.$confirm('你确定要删除吗', '提示')
          await companyDepartmentDelete(this.data.id)
          this.$message.success('删除成功')
          // 刷新页面
          this.$emit('getBranch')
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.item {
  display: flex;
  i {
    margin-right: 5px;
  }
  .title {
    flex: 1;
    color: black;
  }
  .user {
    width: 80px;
    margin-right: 20px;
    text-align: right;
  }
}
</style>
